#include "firstpage.h"
#include "ui_firstpage.h"

FirstPage::FirstPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FirstPage)
{
    ui->setupUi(this);
    QObject::connect(this, SIGNAL(dataUpSignal(QByteArray)),
                     this, SLOT(dataUpSlot(QByteArray)));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(pushButton_3_shuaxin()));
    timer = new QTimer(this);

    // 提前刷新显式控件
    timeoutSlot();

    timer->setInterval(500);   // 设置时间间隔1秒钟
    timer->setSingleShot(false);    // 为周期循环

    // 连接信号槽要在启动定时器之前连接
    connect(timer,SIGNAL(timeout()),this,
        SLOT(timeoutSlot()));

    // 启动定时器
    timer->start();

}

FirstPage::~FirstPage()
{
    delete ui;
}

void FirstPage::paintEvent(QPaintEvent *event)
{
    // 创建一个画家对象
    // 参数为QPaintDevice *表示可绘制的对象
//    QPainter painter(this);

//    QPixmap map(":/new/prefix1/pic/background.jpg");

//    // 绘制图片
//    // 参数1：横轴坐标
//    // 参数2：纵轴坐标
//    // 参数3：绘制的宽度
//    // 参数4：绘制的高度
//    // 参数5：绘制内容
//    painter.drawPixmap(0,0,this->width(),
//                       this->height(),map);

}
//接收数据并显示
void FirstPage::dataUpSlot(QByteArray text)
{
    QString value(text);
    QJsonParseError parseJsonErr;
    QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(),&parseJsonErr);
    QJsonObject jsonObject = document.object();
    qDebug()<<"type="<<jsonObject["type"].toInt();

    if(jsonObject.contains(QStringLiteral("result")))
    {
        QJsonValue jsonValueList = jsonObject.value(QStringLiteral("result"));
        QJsonObject item = jsonValueList.toObject();
        qDebug()<<"result="<<item["result"].toInt();
    }

    if(jsonObject.contains(QStringLiteral("data")))
    {
        QJsonValue arrayValue = jsonObject.value(QStringLiteral("data"));
        if(arrayValue.isArray())
        {
            QJsonArray array = arrayValue.toArray();
            for(int i=0;i<array.size();i++)
            {
                QJsonValue iconArray = array.at(i);
                QJsonObject icon = iconArray.toObject();
                int key = icon["key"].toInt();
                QString val = icon["val"].toString();
                qDebug()<<"key="<<key<<"val="<<val;
                //函数，放结构体里

                //                //结构体->ui
                switch (key) {
                case 301:
                    ui->lineEdit->setText(val);
                    break;
                case 302:
                    ui->lineEdit_2->setText(val);
                    break;
                case 303:
                    ui->lineEdit_4->setText(val);
                    break;
                case 304:
                    if(val == "0")
                    {
                        ui->lineEdit_tf->setText("已关闭");
                        ui->pushButton_fan->setChecked(false);
                    }
                    else
                    {
                        ui->lineEdit_tf->setText("已开启");
                        ui->pushButton_fan->setChecked(true);
                    }
                    break;
                    //                  case 305:
                    //                      ui->lineEdit_3->setText(val);
                    //                      break;
                case 306:
                    if(val == "0")
                    {
                        ui->lineEdit_bj->setText("已关闭");
                        ui->pushButton_baojing->setChecked(false);
                    }
                    else
                    {
                        ui->lineEdit_bj->setText("已开启");
                        ui->pushButton_baojing->setChecked(true);
                    }
                    break;
                case 307:
                    if(val == "0")
                    {
                        ui->lineEdit_8->setText("已关闭");
                        ui->pushButton_led->setChecked(false);
                    }
                    else
                    {
                        ui->lineEdit_8->setText("已开启");
                        ui->pushButton_led->setChecked(true);
                    }
                    break;
                case 308:
                    if(val == "0")
                    {
                        ui->lineEdit_pl->setText("已关闭");
                        ui->pushButton_penlin->setChecked(false);
                    }
                    else
                    {
                        ui->lineEdit_pl->setText("已开启");
                        ui->pushButton_penlin->setChecked(true);
                    }
                    break;
                case 101:
                    if(val == "0")
                    {
                        ui->lineEdit_kt->setText("已关闭");
                        ui->pushButton_kongtiao->setChecked(false);
                    }
                    else
                    {
                        ui->lineEdit_kt->setText("已开启");
                        ui->pushButton_kongtiao->setChecked(true);
                    }
                    break;
                    //                  case 102:
                    //                      ui->lineEdit_3->setText(val);
                    //                      break;
                case 103:
                    ui->lineEdit_7->setText(val);
                    break;
                case 104:
                    ui->lineEdit_3->setText(val);
                    break;
                case 105:
                    ui->lineEdit_6->setText(val);
                    break;
                case 106:
                    ui->lineEdit_5->setText(val);
                    break;



                default:
                    break;
                }

            }
        }
    }
}

void FirstPage::pushButton_3_shuaxin()
{
    QMQTT::Message aaa;
        aaa.setTopic("down");
        aaa.setPayload("{\"type\": 1,\"limit\": \"all\",\"keys\": [301, 302, 303, 304 , 305, 306, 307, 308, 101, 102, 103, 104, 105, 106]}");
        GlobalVal::mqtt->publish(aaa);
}

void FirstPage::timeoutSlot()
{
    // 获取当前系统时间，转换为：时:分:秒
        QString str = QDateTime::currentDateTime().toString("hh:mm:ss");
        ui->lcdNumber->display(str);
}
